<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      position: relative;
      width: 300px;
      margin: 100px auto;
      border-bottom: 1px solid #ccc;
    }
    .box input {
      width: 270px;
      height: 30px;
      border: 0;
      outline: none;
    }
    .box span {
      position: absolute;
      top: 0%;
      right: 0%;
      display: inline-block;
      width: 24px;
      height: 24px;
      /* background-color: pink; */
      background: url(passwords-demo/close.png);
      background-size: 24px auto;
    }
  </style>
</head>
<body>
  <div class="box">
    <input type="password" id="pwd" >
    <span id="eye"></span>
  </div>
  <script>
    let eye = document.getElementById('eye');
    let pwd = document.getElementById('pwd');
    
    let flag = true;
    eye.onclick = function() {
      if (flag) {
        eye.style.backgroundImage = "url(passwords-demo/open.png)";
        // eye.style.backgroundSize = "24px auto"
        pwd.type = "text";
      }else {
        eye.style.backgroundImage = "url(passwords-demo/close.png)";
        pwd.type = "password";
      }
      flag = !flag;
    }
  </script>
</body>
</html>